﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Negocio;
using System.Collections;
using System.Data.SqlClient;
using System.Data;

namespace Presentacion
{
    public partial class Prueba : System.Web.UI.Page
    {
        public static int ModificarId { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                MostrarGrilla(sender, e);
        }

        protected void MostrarGrilla(object sender, EventArgs e)
        {
            Negocio.Prueba prueba = new Negocio.Prueba();
            GridView1.DataSource = prueba.Seleccionar();
            GridView1.DataBind();
            TextBox1.Text = "";
            DropDownList1.SelectedValue = "1";
            CheckBox1.Checked = false;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Negocio.Prueba prueba = new Negocio.Prueba();
            
            if (ModificarId == 0)
            {
                ArrayList Datos = new ArrayList { Convert.ToInt32(DropDownList1.SelectedValue),TextBox1.Text,CheckBox1.Checked,FileUpload1.FileBytes};
                int resultado = prueba.Insertar(Datos);
            }
            else
            {
                ArrayList Datos = new ArrayList { ModificarId, Convert.ToInt32(DropDownList1.SelectedValue), TextBox1.Text, CheckBox1.Checked, FileUpload1.FileBytes };
                int resultado = prueba.Modificar(Datos);
                ModificarId = 0;
            }
            MostrarGrilla(sender, e);
        }

        protected void GridView_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {
            Negocio.Prueba prueba = new Negocio.Prueba();
            int index = Convert.ToInt32(e.CommandArgument);
            DataTable Tabla = prueba.Seleccionar();            
            if (e.CommandName == "Borrar")
            {
                ArrayList DatoId = new ArrayList {Convert.ToInt32(Tabla.Rows[index][0])};
                int resultado = prueba.Borrar(DatoId);
                if (resultado > 0)
                {
                    GridView1.DeleteRow(index);
                    GridView1.DataSource = prueba.Seleccionar();
                    GridView1.DataBind();
                }
                else
                prueba = null;

            }
            if (e.CommandName == "Modificar")
            {
                TextBox1.Text = Tabla.Rows[index][2].ToString();
                DropDownList1.Text = Tabla.Rows[index][1].ToString();
                CheckBox1.Checked = false;
                if (Convert.ToBoolean(Tabla.Rows[index][3]) == true) { CheckBox1.Checked = true; }
                ModificarId = Convert.ToInt32(Tabla.Rows[index][0]);
            }
        }

        protected void GridView_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


        protected void GridView_RowDeleting(object sender, EventArgs e)
        {

        }

        protected void GridView_RowUpdating(object sender, EventArgs e)
        {

        }
    }
}